"""
CSPBuilder plugin

Provides multiplier functionality

Copyright (c) 2008 Rune M. Friborg, runef@diku.dk.
See LICENSE.txt for licensing details (GPLv2).
"""

PLUGIN_NAME = "Multiplier"

import wx

from plugins.builder import *

class PluginMultiplier(PluginBUILDER):

    def __init__(self, pluginManager):
        PluginBUILDER.__init__(self, pluginManager, PLUGIN_NAME)


    def _rcmenu_activate(self, frame, event):
        """ Create dialog for multiplying a process.
        """

        obj = frame.selection[0]
        attr = obj.plugin_attr
        multiplier = 1
        if (attr.has_key('multiplier') and attr['multiplier'] > 1):
            multiplier = attr['multiplier']

        dialog = wx.Dialog(frame, -1, "Choose a multiplier", style = wx.DEFAULT_FRAME_STYLE | wx.WANTS_CHARS )

        win = wx.Panel(dialog, -1)

        panelSizer = wx.BoxSizer(wx.HORIZONTAL)

        spinner = wx.SpinCtrl(win, -1, "", (-1,-1), (100,30))
        spinner.SetRange(1,1000000)
        spinner.SetValue(multiplier)
        panelSizer.Add(spinner, 0, wx.ALL, 5)

        btnOK = wx.Button(win, wx.ID_OK, " Ok ")

        panelSizer.Add(btnOK, 0, wx.ALL | wx.ALIGN_RIGHT, 5)

        win.SetSizer(panelSizer)
        win.SetAutoLayout(True)

        dialog.SetClientSize(wx.Size(210,60))

        if (dialog.ShowModal() == wx.ID_OK):
            obj.plugin_attr['multiplier'] = spinner.GetValue()

        dialog.Destroy()
        frame.drawPanel.Refresh()


    def _rcmenu_multiplier(self, frame, menu, obj, menuid):
        menu.Append(menuid, 'Multiply')
        frame.Bind(wx.EVT_MENU, lambda event:self._rcmenu_activate(frame,event), id=menuid)


    def _check_multiplier(self, mul):
        return (mul > 1)


    def _draw_multiplier(self, dc, obj, position, selected):
        attr = obj.plugin_attr
        if (attr.has_key('multiplier') and attr['multiplier'] > 1):
            dc.DrawRoundedRectangle(position.x + 10, position.y - 18,
                                    obj.size.width, obj.size.height, 8)
            dc.DrawText(str(attr['multiplier']), position.x + (obj.size.width / 2), position.y - 18)               


def load(pluginManager):

    p = PluginMultiplier(pluginManager)

    p.addRightClickElement(location_PROCESS_LINK, p._rcmenu_multiplier)
    p.addCSPAttribute(node_PROCESS_LINK, 'multiplier', int, 1, p._check_multiplier)
    p.addDrawElement(location_PROCESS, 1, p._draw_multiplier)

    pluginManager.addPlugin(p)
